const {
  queryPartsCategoryOptions,
  queryPartsOptionsByPartsCategoryId,
  queryStoreOptionsByPartsId
} = require('../../api/parts')

const {
  to,
  openLoading,
  closeLoading,
  showToast,
  getEventData
} = getApp()
Page({
  data: {
    categoryOptions: [],
    partsOptions: [],
    storeOptions: [],
    currentData: [],
    formData: {
      partsCategoryId: '',
      partsId: '',
      storeId: '',
      inventoryQuantity: '',
      comments: ''
    }
  },
  onLoad() {
    this.getQueryPartsCategoryOptions()
  },

  async getQueryPartsCategoryOptions() {
    openLoading()
    const [err, data] = await to(queryPartsCategoryOptions({}))
    closeLoading()

    if (!err) {
      this.setData({
        categoryOptions: data
      })
    }
  },

  async getQueryPartsOptionsByPartsCategoryId() {
    const {
      partsCategoryId
    } = this.data.formData
    const params = {
      partsCategoryId
    }
    openLoading()
    const [err, data] = await to(queryPartsOptionsByPartsCategoryId(params))
    closeLoading()
    if (!err) {
      this.setData({
        partsOptions: data
      })
    }
  },


  async getQueryStoreOptionsByPartsId() {
    const {
      partsId
    } = this.data.formData
    const params = {
      partsId
    }
    openLoading()
    const [err, data] = await to(queryStoreOptionsByPartsId(params))
    closeLoading()
    if (!err) {
      this.setData({
        storeOptions: data
      })
    }
  },

  onCustomFormTypeChange(event) {
    const {
      type
    } = getEventData(event)
    this.setData({
      [`formData.${type}`]: event.detail
    })
    this.clearForm()

    if (event.detail || event.detail === 0) {
      if (type === 'partsCategoryId') {
        this.getQueryPartsOptionsByPartsCategoryId()
      }

      if (type === 'partsId') {
        const data = this.data.partsOptions.find(item => item.partsId === event.detail)
        this.setData({
          ['formData.partsName']: data.partsName,
          ['formData.partsModel']: data.partsModel,
          ['formData.quantityUnit']: data.quantityUnit,
          currentData: data
        })
        this.getQueryStoreOptionsByPartsId();
      }

      if (type === 'storeId') {
        const data = this.data.storeOptions.find(item => item.storeId === event.detail)
        this.setData({
          ['formData.storeName']: data.storeName,
          ['formData.inventoryQuantity']: data.inventoryQuantity,
        })
      }
    }
  },

  clearForm(type) {
    let clearList = []
    let optionList = []

    if (type === 'partsCategoryId') {
      clearList = ['partsId', 'storeId', 'inventoryQuantity', 'quantityUnit', 'storeName', 'partsModel']
      optionList = ['partsOptions', 'storeOptions']
    }

    if (type === 'partsId') {
      clearList = ['storeId', 'inventoryQuantity', 'partsName', 'storeName']
      optionList = ['partsOptions', 'storeOptions']
    }

    for (const item of clearList) {
      this.setData({
        [`formData.${item}`]: ''
      })
    }

    for (const item of optionList) {
      this.setData({
        [item]: ''
      })
    }
  },

  checkValidate() {
    const {
      partsCategoryId,
      partsId,
      storeId,
      inventoryQuantity
    } = this.data.formData
    if (!partsCategoryId && partsCategoryId !== 0) {
      showToast('请选择备件分类')
      return false
    }

    if (!partsId && partsId !== 0) {
      showToast('请选择备件')
      return false
    }

    if (!storeId && partsId !== 0) {
      showToast('请选择仓库')
      return false
    }

    if (!inventoryQuantity) {
      showToast('暂无库存')
      return false
    }

    return true
  },
  handleSubmit() {
    const isValidate = this.checkValidate()
    if (!isValidate) return
    const choosedData = JSON.parse(this.options.choosedData);
    const item = choosedData.find(item => item.partsId === this.data.formData.partsId)

    if (item) {
      showToast('已选择该备件');
      return
    }

    const pages = getCurrentPages();
    const parentPages = pages[pages.length - 2]
    parentPages.setAttachmentData(this.data.formData)

    wx.navigateBack()
  },
})